﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using TallyBook.BLL.Manger.Expend;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class IncomeUpdateForm : Form
    {
        private readonly IIncomeService<Income> _incomeService;
        private readonly List<TextBox> _textBoxs = new List<TextBox>();
        public  Income Income=new Income();

        public IncomeUpdateForm(IIncomeService<Income> incomeService)
        {
            _incomeService = incomeService;
            InitializeComponent();
        }

        public Action RefreshUpdateMainForm { get; set; }

        private void IncomeUpdateForm_Load(object sender, EventArgs e)
        {

            txtAccount.Text = Income.AccountTypeId.ToString(CultureInfo.InvariantCulture);
            textCurrentMoney.Text = Income.Money.ToString(CultureInfo.InvariantCulture);
            textComment.Text = Income.Comment;

            _textBoxs.Add(txtAccount);
            _textBoxs.Add(textCurrentMoney);
            _textBoxs.Add(textComment);

            var income = new IncomeService();
            comboBoxType.DataSource = null;
            List<IncomeType> getincome = income.GetName();
            comboBoxType.DataSource = getincome;
            comboBoxType.DisplayMember = "Name";
            comboBoxType.ValueMember = "Id";
            comboBoxType.SelectedItem = Income.IncomeTypeId;

        }

        private void btnRegist_Click(object sender, EventArgs e)
        {
            if (_textBoxs.Any(item => string.IsNullOrEmpty(item.Text)))
            {
                MessageBox.Show(@"信息未填完整");
                return;
            }

            var detection = new Detection();
            var list = _textBoxs.Where(item => item.Name != "textCurrentMoney").ToArray();
            if (list.Any(detection.DetectionMoney))
            {
                MessageBox.Show(@"含非法字符");
                return;
            }

            long incometypeid = long.Parse(comboBoxType.SelectedValue.ToString());
            long accountid = long.Parse(txtAccount.Text);
            decimal money = decimal.Parse(textCurrentMoney.Text);
            string comment = textComment.Text;
            DateTime time = DateTime.Now;

            if (_incomeService.Update(incometypeid, accountid, money, comment, time) != 0)
            {
                MessageBox.Show(@"修改成功");
                RefreshUpdateMainForm();
                Hide();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtAccount.Text = null;
            txtAccount.Text = null;
            textCurrentMoney.Text = null;
            textComment.Text = null;
        }

       
    }
}